package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqDeodorizationRunningTwoMapper;
import com.guofen.pojo.SjqDeodorizationRunningTwo;
import com.guofen.service.sjq.SjqDeodorizationRunningTwoService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqDeodorizationRunningTwoServiceImpl implements SjqDeodorizationRunningTwoService {

    @Autowired
    private SjqDeodorizationRunningTwoMapper sjqDeodorizationRunningTwoMapper;

    @Override
    public boolean addForm(String dosingTime1,String waterTime1,String drugName1,double dosage1,String isScaleMark1,String nozzlePipelineCleanTime1,
                           String nozzlePipelineCleanCondition1,String packingCleaningTime1,String packingReplacementTime1,
                           String dosingTime2,String waterTime2,String drugName2,double dosage2,String isScaleMark2,String nozzlePipelineCleanTime2,
                           String nozzlePipelineCleanCondition2,String packingCleaningTime2, String packingReplacementTime2,String reportTime,String operator,String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        SjqDeodorizationRunningTwo sjqDeodorizationRunningTwo = new SjqDeodorizationRunningTwo();
        sjqDeodorizationRunningTwo.setDate(date);
        sjqDeodorizationRunningTwo.setDosingTime1(dosingTime1);
        sjqDeodorizationRunningTwo.setWaterTime1(waterTime1);
        sjqDeodorizationRunningTwo.setDrugName1(drugName1);
        sjqDeodorizationRunningTwo.setDosage1(dosage1);
        sjqDeodorizationRunningTwo.setIsScaleMark1(isScaleMark1);
        sjqDeodorizationRunningTwo.setNozzlePipelineCleanTime1(nozzlePipelineCleanTime1);
        sjqDeodorizationRunningTwo.setNozzlePipelineCleanCondition1(nozzlePipelineCleanCondition1);
        sjqDeodorizationRunningTwo.setPackingCleaningTime1(packingCleaningTime1);
        sjqDeodorizationRunningTwo.setPackingReplacementTime1(packingReplacementTime1);
        sjqDeodorizationRunningTwo.setDosingTime2(dosingTime2);
        sjqDeodorizationRunningTwo.setWaterTime2(waterTime2);
        sjqDeodorizationRunningTwo.setDrugName2(drugName2);
        sjqDeodorizationRunningTwo.setDosage2(dosage2);
        sjqDeodorizationRunningTwo.setIsScaleMark2(isScaleMark2);
        sjqDeodorizationRunningTwo.setNozzlePipelineCleanTime2(nozzlePipelineCleanTime2);
        sjqDeodorizationRunningTwo.setNozzlePipelineCleanCondition2(nozzlePipelineCleanCondition2);
        sjqDeodorizationRunningTwo.setPackingCleaningTime2(packingCleaningTime2);
        sjqDeodorizationRunningTwo.setPackingReplacementTime2(packingReplacementTime2);
        sjqDeodorizationRunningTwo.setReportTime(reportTime);
        sjqDeodorizationRunningTwo.setOperator(operator);
        sjqDeodorizationRunningTwo.setRemarks(remarks);

        int result = sjqDeodorizationRunningTwoMapper.insert(sjqDeodorizationRunningTwo);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqDeodorizationRunningTwo findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        SjqDeodorizationRunningTwo sjqDeodorizationRunningOne = sjqDeodorizationRunningTwoMapper.findLast();
        System.out.println("最新记录" + sjqDeodorizationRunningTwoMapper.findLast());
        //获取最新记录日期
        String dateLast =  sjqDeodorizationRunningOne.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return sjqDeodorizationRunningOne;
        else
            return null;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqDeodorizationRunningTwo> list = sjqDeodorizationRunningTwoMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqDeodorizationRunningTwo> list = sjqDeodorizationRunningTwoMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqDeodorizationRunningTwoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqDeodorizationRunningTwo record) {
        return sjqDeodorizationRunningTwoMapper.updateByPrimaryKeySelective(record);
    }
}
